/*
 * Created by JFormDesigner on Wed Jul 06 21:31:16 EDT 2011
 */

package view;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;


/**
 * @author Joe Labrosse
 */
public class BranchSuccursale extends JFrame implements AbstractBranchUI {
	public BranchSuccursale() {
		initComponents();
	}

	private void snapshitThos(ActionEvent e) {
		for( SnapshotListener l : listeners) {
			l.snapshotRequested();
		}
	}

	private void transferMoney(ActionEvent e) {
		int amount = Integer.parseInt(txtMoneyTransfer.getText());
		
			for(MoneyTransferListener listener : listenersTransfer) {
				listener.sendMoney(txtBankTo.getText(), amount);
			}
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Joe Labrosse
		button2 = new JButton();
		scrollPane1 = new JScrollPane();
		textLog = new JTextArea();
		label1 = new JLabel();
		txtMoneyTransfer = new JTextField();
		label2 = new JLabel();
		txtBankTo = new JTextField();
		button3 = new JButton();
		label3 = new JLabel();
		txtMoney = new JTextField();

		//======== this ========
		setResizable(false);
		Container contentPane = getContentPane();

		//---- button2 ----
		button2.setText("Capture Snapshot");
		button2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				snapshitThos(e);
			}
		});

		//======== scrollPane1 ========
		{
			scrollPane1.setViewportView(textLog);
		}

		//---- label1 ----
		label1.setText("Transf\u00e9rer:");
		label1.setHorizontalAlignment(SwingConstants.RIGHT);

		//---- label2 ----
		label2.setText("Vers:");
		label2.setHorizontalAlignment(SwingConstants.RIGHT);

		//---- txtBankTo ----
		txtBankTo.setText("A");

		//---- button3 ----
		button3.setText("Transf\u00e9rer");
		button3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				transferMoney(e);
			}
		});

		//---- label3 ----
		label3.setText("Somme:");
		label3.setHorizontalAlignment(SwingConstants.RIGHT);

		//---- txtMoney ----
		txtMoney.setEditable(false);

		GroupLayout contentPaneLayout = new GroupLayout(contentPane);
		contentPane.setLayout(contentPaneLayout);
		contentPaneLayout.setHorizontalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(contentPaneLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(contentPaneLayout.createParallelGroup()
						.addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 589, Short.MAX_VALUE)
						.addGroup(contentPaneLayout.createSequentialGroup()
							.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
								.addComponent(label2, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(label1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addGroup(contentPaneLayout.createSequentialGroup()
									.addGap(2, 2, 2)
									.addComponent(label3, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE)))
							.addGroup(contentPaneLayout.createParallelGroup()
								.addGroup(contentPaneLayout.createSequentialGroup()
									.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
									.addComponent(txtMoney, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE))
								.addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
									.addGap(18, 18, 18)
									.addGroup(contentPaneLayout.createParallelGroup()
										.addComponent(txtBankTo, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
										.addComponent(txtMoneyTransfer, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE))))
							.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
							.addComponent(button3, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
							.addGap(18, 18, 18)
							.addComponent(button2, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		contentPaneLayout.setVerticalGroup(
			contentPaneLayout.createParallelGroup()
				.addGroup(contentPaneLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(contentPaneLayout.createParallelGroup()
						.addGroup(contentPaneLayout.createSequentialGroup()
							.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
								.addComponent(txtMoney, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(label3))
							.addGap(3, 3, 3)
							.addGroup(contentPaneLayout.createParallelGroup()
								.addComponent(txtMoneyTransfer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addGroup(contentPaneLayout.createSequentialGroup()
									.addGap(5, 5, 5)
									.addComponent(label1)))
							.addGroup(contentPaneLayout.createParallelGroup()
								.addGroup(contentPaneLayout.createSequentialGroup()
									.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
									.addComponent(txtBankTo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGroup(contentPaneLayout.createSequentialGroup()
									.addGap(11, 11, 11)
									.addComponent(label2))))
						.addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
							.addComponent(button2, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
							.addComponent(button3, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)))
					.addGap(15, 15, 15)
					.addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
					.addContainerGap())
		);
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Joe Labrosse
	private JButton button2;
	private JScrollPane scrollPane1;
	private JTextArea textLog;
	private JLabel label1;
	private JTextField txtMoneyTransfer;
	private JLabel label2;
	private JTextField txtBankTo;
	private JButton button3;
	private JLabel label3;
	private JTextField txtMoney;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
	@Override
	public void setUIName(final String nom) {
		SwingUtilities.invokeLater( new Runnable()
		{
			public void run() {
				setTitle( nom );
			}
		} );		
	}

	@Override
	public void setAmount(final int montant) {
		SwingUtilities.invokeLater( new Runnable()
		{
			public void run() {
				txtMoney.setText( "" + montant);
			}
		} );
	}

	@Override
	public void showMessage(final String string) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater( new Runnable()
		{
			public void run()
			{
				
				for( String ligne : string.split( "\n", -1 ) )
				{
					if( !ligne.isEmpty() )
					{
						textLog.setText(textLog.getText() + "\n");
						textLog.setText(textLog.getText() + ligne);
					}
					else
					{
						textLog.setText(textLog.getText() + "\n");
					}
				}
				
			}
		} );
	}

	@Override
	public void showError(String string) {
		// TODO Auto-generated method stub
		
	}

	private final ArrayList<SnapshotListener> listeners = new ArrayList<SnapshotListener>();
	private final ArrayList<MoneyTransferListener> listenersTransfer = new ArrayList<MoneyTransferListener>();

	@Override
	public void addSnapshotListener(SnapshotListener listener) {
		// TODO Auto-generated method stub
		listeners.add( listener );
	}
	@Override
	public void removeSnapshotListener(SnapshotListener listener) {
		// TODO Auto-generated method stub
		listeners.remove(listener);
	}

	@Override
	public void addTransferMoneyListener(MoneyTransferListener listener) {
		listenersTransfer.add(listener);
	}

	@Override
	public void removeTransferMoneyListener(MoneyTransferListener listener) {
		// TODO Auto-generated method stub
		listenersTransfer.remove(listener);
	}
}
